package com.yksj.healthtalk.ui.doctorstation;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.comm.SingleBtnFragmentDialog;
import com.yksj.healthtalk.net.http.AsyncHttpResponseHandler;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.SystemUtils;
import com.yksj.healthtalk.utils.ToastUtil;
/**
 * 普通咨询子类
 * @author Administrator
 *
 */
public class DoctorCommonTalkOption extends BaseFragmentActivity implements OnClickListener, OnCheckedChangeListener{

	private EditText mServiceMoney;//服务价格
	private EditText mServiceNum;//服务名额
	private DataEntity entity;
	private ToggleButton mOpenStatus;
	private String SERVICE_TYPE_SUB_ID;
	private boolean isUpdate=false;
	private boolean isChange=false;
	private TextView serviceIsSeclect;
	private Boolean isEndUpdate=false;//最终结果
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.doctor_common_talk_option);
		initTitle();
		initView();
	}

	private void initView() {
		titleLeftBtn.setOnClickListener(this);
		
		titleTextV.setText((getIntent().getStringExtra("title").split("（"))[0]);
		findViewById(R.id.special_layout).setOnClickListener(this);
		findViewById(R.id.service_content).setOnClickListener(this);
		mServiceMoney=(EditText)findViewById(R.id.service_money);
		mServiceNum=(EditText)findViewById(R.id.service_num);
		titleRightBtn2.setVisibility(View.VISIBLE);
		titleRightBtn2.setText("确定");
		titleRightBtn2.setOnClickListener(this);
		SERVICE_TYPE_SUB_ID=getIntent().getStringExtra("SERVICE_TYPE_SUB_ID");
		initData(SERVICE_TYPE_SUB_ID);
	}

	private void initData(String id) {
		HttpRestClient.doHttpQureyDocotorCommonOption(id,new AsyncHttpResponseHandler(this){
			@Override
			public void onSuccess(String content) {
				super.onSuccess(content);
				try {
					JSONObject object=new JSONObject(content);
					if(object.has("error_message")){
						ToastUtil.showBasicShortToast(getApplicationContext(), object.optString("error_message"));
					}else{
						if(isChange){
							serviceIsSeclect.setText(object.optString("ISSLECT","0").equals("0")?"未选择":"已选择");
						}else{
							entity=new DataEntity(object.optString("SERVICE_ITEM_ID"),object.optString("SERVICE_MAX"), 
							object.optString("REGISTEREDMAX"),object.optString("SERVICE_PRICE"),object.optString("SERVERMAX"),
							object.optString("SERVERLEA"),object.optString("ORDER_ON_OFF","0"),object.optString("REGISTEREDLEA"),object.optString("ISSLECT","0"));
							initView2();
						}
					}
				} catch (Exception e) {}
			}
		});
		
	}
	
	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		super.onActivityResult(arg0, arg1, arg2);
		isChange=true;
		initData(SERVICE_TYPE_SUB_ID);
	}
	
	protected void initView2() {
		
		mServiceMoney.setText(entity.getSERVICE_PRICE());
		mServiceNum.setText(entity.getSERVICE_MAX());
		
		mOpenStatus = (ToggleButton)findViewById(R.id.service_open);
		mOpenStatus.setOnCheckedChangeListener(this);
		mOpenStatus.setChecked("1".equals(entity.getORDER_ON_OFF()));
		serviceIsSeclect = (TextView)findViewById(R.id.service_isselect);
		serviceIsSeclect.setText(entity.getISSLECT().equals("0")?"未选择":"已选择");
		
		TextView erviceText= (TextView)findViewById(R.id.service_name);
		TextView moneyText= (TextView)findViewById(R.id.money_name);
		erviceText.setText("服务名额("+entity.getREGISTEREDLEA()+"-"+entity.getREGISTEREDMAX()+"人)");
		moneyText.setText("服务价格("+entity.getSERVERLEA()+"-"+entity.getSERVERMAX()+"元/人)");
		
		isEndUpdate=mOpenStatus.isChecked();
		
	}

	@Override
	public void onClick(View v) {
		Intent intent;
		switch (v.getId()) {
		case R.id.title_back:
			Intent intent2 = getIntent();
			if(isUpdate){
				intent2.putExtra("SERVICE_TYPE_SUB_ID",SERVICE_TYPE_SUB_ID);
//				intent2.putExtra("flag",mOpenStatus.isChecked()?1:0);
				intent2.putExtra("flag",isEndUpdate?1:0);
				setResult(RESULT_OK, intent2);
			}else{
				setResult(RESULT_CANCELED);
			}
			finish();
			break;
		case R.id.special_layout://特殊收费人群
			intent = new Intent(this,DoctorSpecialServiceListActivity.class);
			intent.putExtra("SERVICE_ITEM_ID", entity.getSERVICE_ITEM_ID());
			startActivity(intent);
			break;
		case R.id.service_content://服务内容
			intent=new Intent(this,DoctorServiceContentList.class);
			intent.putExtra("SERVICE_ITEM_ID", entity.getSERVICE_ITEM_ID());
			intent.putExtra("SERVICE_TYPE_SUB_ID", SERVICE_TYPE_SUB_ID);
			intent.putExtra("SERVICE_TYPE_ID", getIntent().getStringExtra("SERVICE_TYPE_ID"));
			startActivityForResult(intent,1000);
			break;
		case R.id.title_right2:
			SystemUtils.hideSoftBord(getApplicationContext(), mServiceMoney);
			boolean sumint=false;
			if(mOpenStatus.isChecked()){
				if("".equals(serviceIsSeclect.getText().toString())||serviceIsSeclect.getText().toString().equals("未选择")){
					sumint=true;
				}
			}
			
			if(sumint){
				ToastUtil.showShort("请选择服务内容");
				return;
			}
			
			int mSmoney;
			int mSNum;
			try {
				mSNum = Integer.valueOf(mServiceNum.getText().toString());
				if(Integer.valueOf(entity.getREGISTEREDLEA())>mSNum||mSNum>Integer.valueOf(entity.getREGISTEREDMAX())){
					ToastUtil.showBasicShortToast(getApplicationContext(), "服务名额输入有误");
					return;
				}
			} catch (Exception e) {
				ToastUtil.showBasicShortToast(getApplicationContext(), "服务名额输入有误");
				return;
			}
			
			try {
				mSmoney = Integer.valueOf(mServiceMoney.getText().toString());
				if(Integer.valueOf(entity.getSERVERLEA())>mSmoney||mSmoney>Integer.valueOf(entity.getSERVERMAX())){
					ToastUtil.showBasicShortToast(getApplicationContext(), "服务价格输入有误");
					return;
				}
			} catch (Exception e) {
				ToastUtil.showBasicShortToast(getApplicationContext(), "服务价格输入有误");
				return;
			}
			
			HttpRestClient.doHttpSubmitDocotorService(entity.getSERVICE_ITEM_ID(),mOpenStatus.isChecked(),mSmoney,mSNum,new AsyncHttpResponseHandler(this){
				@Override
				public void onSuccess(String content) {
					super.onSuccess(content);
					try {
						JSONObject object=new JSONObject(content);
						if(object.has("error_message")){
							ToastUtil.showBasicShortToast(getApplicationContext(), object.optString("error_message"));
						}else{
							isUpdate=true;
							SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(), object.getString("message"));
							isEndUpdate=mOpenStatus.isChecked();
						}
					} catch (Exception e) {}
				}
			});
			break;
		}
	}
	
	
	class DataEntity{
		private String SERVICE_ITEM_ID;//医生服务项目ID
		private String SERVICE_MAX;//服务最大名额
		private String SERVICE_PRICE;//服务价格
		private String REGISTEREDMAX;//医生挂号名额上限
		private String REGISTEREDLEA;//医生服务名额下限
		private String SERVERMAX;//医生服务价格上限
		private String SERVERLEA;//医生服务价格下限
		private String ORDER_ON_OFF;//开启标记: 1-开；0-关
		private String ISSLECT;//服务内容是否选择
		
		public String getISSLECT() {
			return ISSLECT;
		}
		public void setISSLECT(String iSSLECT) {
			ISSLECT = iSSLECT;
		}
		public String getSERVICE_ITEM_ID() {
			return SERVICE_ITEM_ID;
		}
		public void setSERVICE_ITEM_ID(String sERVICE_ITEM_ID) {
			SERVICE_ITEM_ID = sERVICE_ITEM_ID;
		}
		public String getSERVICE_MAX() {
			return SERVICE_MAX;
		}
		public void setSERVICE_MAX(String sERVICE_MAX) {
			SERVICE_MAX = sERVICE_MAX;
		}
		public String getREGISTEREDMAX() {
			return REGISTEREDMAX;
		}
		public void setREGISTEREDMAX(String rEGISTEREDMAX) {
			REGISTEREDMAX = rEGISTEREDMAX;
		}
		public String getSERVICE_PRICE() {
			return SERVICE_PRICE;
		}
		public void setSERVICE_PRICE(String sERVICE_PRICE) {
			SERVICE_PRICE = sERVICE_PRICE;
		}
		public String getSERVERMAX() {
			return SERVERMAX;
		}
		public void setSERVERMAX(String sERVERMAX) {
			SERVERMAX = sERVERMAX;
		}
		public String getSERVERLEA() {
			return SERVERLEA;
		}
		public void setSERVERLEA(String sERVERLEA) {
			SERVERLEA = sERVERLEA;
		}
		public String getORDER_ON_OFF() {
			return ORDER_ON_OFF;
		}
		public void setORDER_ON_OFF(String oRDER_ON_OFF) {
			ORDER_ON_OFF = oRDER_ON_OFF;
		}
		public String getREGISTEREDLEA() {
			return REGISTEREDLEA;
		}
		public void setREGISTEREDLEA(String rEGISTEREDLEA) {
			REGISTEREDLEA = rEGISTEREDLEA;
		}
		
		public DataEntity(String sERVICE_ITEM_ID, String sERVICE_MAX,
				String rEGISTEREDMAX, String sERVICE_PRICE, String sERVERMAX,
				String sERVERLEA, String oRDER_ON_OFF, String rEGISTEREDLEA,String ISSLECT) {
			super();
			this.SERVICE_ITEM_ID = sERVICE_ITEM_ID;
			this.SERVICE_MAX = sERVICE_MAX;
			this.REGISTEREDMAX = rEGISTEREDMAX;
			this.SERVICE_PRICE = sERVICE_PRICE;
			this.SERVERMAX = sERVERMAX;
			this.SERVERLEA = sERVERLEA;
			this.ORDER_ON_OFF = oRDER_ON_OFF;
			this.REGISTEREDLEA = rEGISTEREDLEA;
			this.ISSLECT=ISSLECT;
		}
		
	}


	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if(isChecked){
			findViewById(R.id.line_layout).setVisibility(View.VISIBLE);
		}else{
			findViewById(R.id.line_layout).setVisibility(View.GONE);
		}
		
	}
}
